package com.zzh.bc.service.project.impl;

import com.zzh.bc.dao.project.ProjectDao;
import com.zzh.bc.dao.project.impl.ProjectDaoImpl;
import com.zzh.bc.model.PageModel;
import com.zzh.bc.model.ProjectModel;
import com.zzh.bc.modules.project.vo.ProjectVo;
import com.zzh.bc.service.project.ProjectService;

import java.util.List;
import java.util.UUID;

public class ProjectServiceImpl implements ProjectService {
    private ProjectDao projectDao;

    @Override
    public List<ProjectModel> findAllProject(int currentPage, int pageSize, String userId) {
        checkProjectDao();
        PageModel model = new PageModel();
        model.setOffset(currentPage * pageSize);
        model.setPageSize(pageSize);
        return projectDao.findObjByUserId(model, userId);
    }

    @Override
    public ProjectModel findProject(String projectId, String userId) {
        return null;
    }

    @Override
    public boolean saveProject(ProjectVo vo) {
        ProjectModel model = new ProjectModel();
        model.setCreatedBy(vo.getCreateUserId());
        model.setCreatedTime(System.currentTimeMillis());
        model.setProjectBaseUrl(vo.getProjectBaseUrl());
        model.setProjectCover(vo.getProjectCover());
        model.setProjectId(UUID.randomUUID().toString());
        model.setProjectName(vo.getProjectName());
        model.setProjectDetails(vo.getProjectDetails());
        model.setUpdatedBy(vo.getCreateUserId());
        model.setUpdatedTime(model.getCreatedTime());
        model.setCreateUserId(model.getCreateUserId());
        checkProjectDao();
        return projectDao.save(model);
    }


    @Override
    public boolean deleteProject(String currentUserId, ProjectVo vo) {
        checkProjectDao();
        ProjectModel obj = projectDao.findObjById(vo.getProjectId(), currentUserId);
        return projectDao.deleteProject(obj);
    }

    private void checkProjectDao() {
        if (projectDao == null) {
            projectDao = new ProjectDaoImpl();
        }
    }
}
